package com.resourcestore.action;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.List;

import com.resourcestore.utils.CommonUtil;
import com.resourcestore.action.BaseAction;
import com.resourcestore.dao.CourseCategoryDao;
import com.resourcestore.model.CourseCategory;
import com.resourcestore.utils.tag.Pagination;

public class CourseCategoryAction extends BaseAction {

	private CourseCategory courseCategory;
	private List<CourseCategory> listCourseCategory;
	private CourseCategoryDao courseCategoryDao = new CourseCategoryDao();
	private Pagination pagination = new Pagination();

	/**
	 * 查询返回List
	 * 
	 * @throws ParseException
	 * 
	 */
	public String listCourseCategory() {

		courseCategory = new CourseCategory();
		courseCategory.setIsPublic(-1);
		pagination.setTotal(courseCategoryDao
				.queryCourseCategoryNum(courseCategory));
		listCourseCategory = courseCategoryDao.selectCourseCategoryPage(
				courseCategory, pagination);

		for (int i = 0; i < listCourseCategory.size(); i++) {
			if (listCourseCategory.get(i).getIsPublic() == 0) {
				listCourseCategory.get(i).setIsPublicString("否");
			} else if (listCourseCategory.get(i).getIsPublic() == 1) {
				listCourseCategory.get(i).setIsPublicString("是");
			}
			try {
				listCourseCategory.get(i).setGmtCreatedString(
						CommonUtil.getSysTimeFormatString(listCourseCategory
								.get(i).getGmtCreated()));
				listCourseCategory.get(i).setGmtModifiedString(
						CommonUtil.getSysTimeFormatString(listCourseCategory
								.get(i).getGmtModified()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return "default";
	}

	/**
	 * 根据条件搜索，查询返回List带分页
	 * 
	 * @throws ParseException
	 * 
	 */
	public String searchListCourseCategory() {

		// 对内容进行urldecoder解码
		try {
			courseCategory.setCourseCateName(new String(courseCategory
					.getCourseCateName().getBytes("iso-8859-1"), "UTF-8"));
			courseCategory.setCourseCateRemark(new String(courseCategory.getCourseCateRemark()
					.getBytes("iso-8859-1"), "UTF-8"));
			courseCategory.setAuthor(new String(courseCategory.getAuthor()
					.getBytes("iso-8859-1"), "UTF-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		pagination.setTotal(courseCategoryDao
				.queryCourseCategoryNum(courseCategory));
		listCourseCategory = courseCategoryDao.selectCourseCategoryPage(
				courseCategory, pagination);

		// 将bigint数字处理为时间格式

		for (int i = 0; i < listCourseCategory.size(); i++) {
			if (listCourseCategory.get(i).getIsPublic() == 0) {
				listCourseCategory.get(i).setIsPublicString("否");
			} else if (listCourseCategory.get(i).getIsPublic() == 1) {
				listCourseCategory.get(i).setIsPublicString("是");
			}
			try {
				listCourseCategory.get(i).setGmtCreatedString(
						CommonUtil.getSysTimeFormatString(listCourseCategory
								.get(i).getGmtCreated()));
				listCourseCategory.get(i).setGmtModifiedString(
						CommonUtil.getSysTimeFormatString(listCourseCategory
								.get(i).getGmtModified()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return newpath("/CourseCategory/listCourseCategory.jsp");
	}

	/**
	 * 预插入一条数据
	 */
	public String preInsertCourseCategory() {

		return newpath("/CourseCategory/insertCourseCategory.jsp");
	}

	/**
	 * 插入一条数据
	 */
	public String insertCourseCategory() {

		try {
			courseCategory.setGmtCreated(CommonUtil.getSysTimeFormatNum());
			courseCategory.setGmtModified(CommonUtil.getSysTimeFormatNum());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		courseCategoryDao.insertCourseCategory(courseCategory);
		return redirect(this.getUrl());
	}

	/**
	 * 更新之前加载原信息
	 * 
	 */
	public String preUpdateCourseCategory() {
		courseCategory = courseCategoryDao
				.selectCourseCategoryById(courseCategory);

		return newpath("/CourseCategory/updateCourseCategory.jsp");
	}

	/**
	 * 更新信息
	 * 
	 */
	public String updateCourseCategory() {

		try {
			// courseCategory.setGmtCreated(CommonUtil.getSysTimeFormatNum());
			courseCategory.setGmtModified(CommonUtil.getSysTimeFormatNum());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		courseCategoryDao.updateCourseCategory(courseCategory);
		return redirect(this.getUrl());
	}

	/**
	 * 删除信息
	 */
	public String deleteCourseCategory() {
		courseCategoryDao.deleteCourseCategory(courseCategory);
		return redirect(this.getUrl());
	}

	/**
	 * 批量删除信息
	 */
	public String deleteMoreCourseCategory() {
	
		courseCategoryDao.deleteMoreCourseCategory(courseCategory);
		return redirect(this.getUrl());
	}

	public CourseCategory getCourseCategory() {
		return courseCategory;
	}

	public void setCourseCategory(CourseCategory courseCategory) {
		this.courseCategory = courseCategory;
	}

	public List<CourseCategory> getListCourseCategory() {
		return listCourseCategory;
	}

	public void setListCourseCategory(List<CourseCategory> listCourseCategory) {
		this.listCourseCategory = listCourseCategory;
	}

	public Pagination getPagination() {
		return pagination;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}

}
